package com.tingyun.sign.admin.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.networkbench.base.util.security.MD5;
import com.tingyun.sign.admin.bean.ScUManagerDto;
import com.tingyun.sign.admin.service.ManagerService;
import com.tingyun.sign.core.entity.ScUManagerBean;
import com.tingyun.sign.core.repository.ScUManagerDao;
import com.tingyun.sign.utils.MyAssert;
import com.tingyun.sign.utils.Page;

@Service
public class ManagerServiceImpl implements ManagerService {

	private static Logger log = LoggerFactory
			.getLogger(ManagerServiceImpl.class);

	@Autowired
	private ScUManagerDao scUManagerDao;

	/**
	 * 检查用户登录
	 * 
	 * @return 0:用户名不存在，1：密码错误，2：校验通过
	 */
	@Override
	public Integer login(ScUManagerBean bean) {
		MyAssert.notNull(bean, "登录信息为空");
		MyAssert.notNull(bean.getManagerName(), "[用户名]为空");
		MyAssert.notNull(bean.getPassWord(), "[密码]为空");
		// 查询名称是否存在
		Integer count = scUManagerDao.findByName(bean.getManagerName());
		if (count == null || count == 0) {
			return 0;
		}
		// 查询名称和加密后的密码是否匹配
		ScUManagerBean obj = scUManagerDao.findByNameAndPwd(
				bean.getManagerName(), MD5.encryptToHex(bean.getPassWord()));
		if (obj == null) {// 密码错误
			return 1;
		} else {// 密码正确
			return 2;
		}
	}

	/**
	 * 根据名字和密码查找用户信息
	 */
	public ScUManagerBean findByNameAndPass(String name, String pass) {
		return scUManagerDao.findByNameAndPwd(name, MD5.encryptToHex(pass));
	}

	/**
	 * 添加管理员
	 */
	public Integer addManager(ScUManagerBean bean) {
		// 校验参数
		MyAssert.notNull(bean, "参数为空");
		MyAssert.notNull(bean.getManagerName(), "[用户名]为空");
		MyAssert.notNull(bean.getPassWord(), "[密码]为空");
		MyAssert.notNull(bean.getType(), "[管理员类型]为空");
		if (bean.getManagerName().length() > 30) {
			MyAssert.isTrue(false, "管理员名称长度超过限制，应小于30个英文字符");
		}
		if (bean.getPassWord().length() < 6) {
			MyAssert.isTrue(false, "密码长度不能小于6");
		}
		// 密码加密
		bean.setPassWord(MD5.encryptToHex(bean.getPassWord()));
		// 校验管理员名称是否重复
		Integer count = scUManagerDao.findByName(bean.getManagerName());
		if (count != null && count > 0) {
			MyAssert.isTrue(false, "管理员名称[" + bean.getManagerName() + "]已存在");
		}
		try {
			scUManagerDao.save(bean);
		} catch (Exception e) {
			log.error("添加管理员失败", e);
			MyAssert.isTrue(false, "数据库异常，添加管理员失败，请稍后重试");
		}
		return 1;
	}

	/**
	 * 编辑管理员信息
	 */
	@Override
	@Transactional
	public ScUManagerBean editManager(ScUManagerDto bean) {
		// 校验参数
		MyAssert.notNull(bean, "参数为空");
		MyAssert.notNull(bean.getManagerName(), "[用户名]为空");
		MyAssert.notNull(bean.getPassWord(), "[新密码]为空");
		MyAssert.notNull(bean.getNewPassWord2(), "[重复输入新密码]为空");
		MyAssert.isTrue(bean.getPassWord().equals(bean.getNewPassWord2()),
				"两次输入的新密码不一致");
		// 根据账号和密码查询是否存在
		String nameString = bean.getManagerName();
		if (scUManagerDao.findByName(nameString) == 1) {// 旧密码正确
			try {
				scUManagerDao.editManager(bean.getManagerName(),bean.getStatus(),bean.getType(),bean.getMuser(),
						MD5.encryptToHex(bean.getPassWord()));
			} catch (Exception e) {
				log.error("添加管理员失败", e);
				MyAssert.isTrue(false, "数据库异常，修改密码失败，请稍后重试");
			}
		}
		// 查询新的管理员信息
		ScUManagerBean newBean = scUManagerDao.findByNameAndPwd(
				bean.getManagerName(), MD5.encryptToHex(bean.getPassWord()));
		return newBean;
	}

	@Override
	public Page<ScUManagerBean> findAllManageres() {
		Page<ScUManagerBean> page = new Page<ScUManagerBean>();
		List<ScUManagerBean> list = scUManagerDao.findAllManagers();
		page.setResultList(list);
		return page;
	}

	@Override
	@Transactional
	public Integer deleteById(ScUManagerBean bean) {
		MyAssert.notNull(bean, "参数为空");
		MyAssert.notNull(bean.getId(), "[登录ID]为空");
		try {
			scUManagerDao.deleteById(bean.getId(), bean.getMuser());
		} catch (Exception e) {
			log.error("删除管理员失败", e);
			MyAssert.isTrue(false, "系统异常，删除管理员失败，请稍后重试");
		}

		return 1;
	}

	@Override
	public Integer checkByType() {
		return scUManagerDao.checkByType();
	}

	@Override
	public boolean findByName(String string) {
		int count = scUManagerDao.findByName(string);
		if (count == 1) {
			return true;
		} else {
			return false;
		}
	}

}
